<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>一个easyui的数据列表栗子</title>
    <!-- 引入easyui库 -->
    <!-- 新建一个html页面，在页面中引入本地的easyui框架 -->
    <link rel="stylesheet" type="text/css"
          href="../js/jquery-easyui-1.7.0/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css"
          href="../js/jquery-easyui-1.7.0/themes/icon.css">
    <link rel="stylesheet" type="text/css"
          href="../js/jquery-easyui-1.7.0/themes/color.css">
    <script type="text/javascript"
            src="../js/jquery-easyui-1.7.0/jquery.min.js"></script>
    <script type="text/javascript"
            src="../js/jquery-easyui-1.7.0/jquery.easyui.min.js"></script>
</head>
<body>

<!-- 使用eauiui进行数据列表设计  start   -->

<table id="demo_dg" title="My Users" class="easyui-datagrid"
       style="width: 700px; height: 450px" url="../js/userlist.json"
       toolbar="#demo_toolbar" pagination="true" rownumbers="true"
       fitColumns="true" fit="true" singleSelect="true">
    <thead>
    <tr>
        <th field="firstname" width="50">First Name</th>
        <th field="lastname" width="50">Last Name</th>
        <th field="phone" width="50">Phone</th>
        <th field="email" width="50">Email</th>
    </tr>
    </thead>
</table>
<div id="demo_toolbar">
    <a href="javascript:void(0)" class="easyui-linkbutton"
       iconCls="icon-add" plain="true" onclick="newUser()">New User</a> <a
        href="javascript:void(0)" class="easyui-linkbutton"
        iconCls="icon-edit" plain="true" onclick="editUser()">Edit User</a> <a
        href="javascript:void(0)" class="easyui-linkbutton"
        iconCls="icon-remove" plain="true" onclick="destroyUser()">Remove
    User</a>
</div>

<div id="demo_dlg" class="easyui-dialog" style="width: 400px"
     data-options="closed:true,modal:true,border:'thin',buttons:'#demo_dlg-buttons'">
    <form id="demo_fm" method="post" novalidate
          style="margin: 0; padding: 20px 50px">
        <h3>User Information</h3>
        <div style="margin-bottom: 10px">
            <input name="firstname" class="easyui-textbox" required="true"
                   label="First Name:" style="width: 100%">
        </div>
        <div style="margin-bottom: 10px">
            <input name="lastname" class="easyui-textbox" required="true"
                   label="Last Name:" style="width: 100%">
        </div>
        <div style="margin-bottom: 10px">
            <input name="phone" class="easyui-textbox" required="true"
                   label="Phone:" style="width: 100%">
        </div>
        <div style="margin-bottom: 10px">
            <input name="email" class="easyui-textbox" required="true"
                   validType="email" label="Email:" style="width: 100%">
        </div>
    </form>
</div>
<div id="demo_dlg-buttons">
    <a href="javascript:void(0)" class="easyui-linkbutton c6"
       iconCls="icon-ok" onclick="saveUser()" style="width: 90px">Save</a> <a
        href="javascript:void(0)" class="easyui-linkbutton"
        iconCls="icon-cancel" onclick="javascript:$('#demo_dlg').dialog('close')"
        style="width: 90px">Cancel</a>
</div>
<!-- 使用eauiui进行数据列表设计  end   -->

<script type="text/javascript">
    var url;
    function newUser(){
        $('#demo_dlg').dialog('open').dialog('center').dialog('setTitle','New User');
        $('#demo_fm').form('clear');
        url = 'save_user.php';
    }
    function editUser(){
        var row = $('#demo_dg').datagrid('getSelected');
        if (row){
            $('#demo_dlg').dialog('open').dialog('center').dialog('setTitle','Edit User');
            $('#demo_fm').form('load',row);
            url = 'update_user.php?id='+row.id;
        }
    }
    function saveUser(){
        $('#demo_fm').form('submit',{
            url: url,
            /* iframe: false, */
            onSubmit: function(){
                return $(this).form('validate');
            },
            success: function(result){
                var result = eval('('+result+')');
                if (result.errorMsg){
                    $.messager.show({
                        title: 'Error',
                        msg: result.errorMsg
                    });
                } else {
                    $('#demo_dlg').dialog('close');		// close the dialog
                    $('#demo_dg').datagrid('reload');	// reload the user data
                }
            }
        });
    }
    function destroyUser(){
        var row = $('#demo_dg').datagrid('getSelected');
        if (row){
            $.messager.confirm('Confirm','Are you sure you want to destroy this user?',function(r){
                if (r){
                    $.post('destroy_user.php',{id:row.id},function(result){
                        if (result.success){
                            $('#demo_dg').datagrid('reload');	// reload the user data
                        } else {
                            $.messager.show({	// show error message
                                title: 'Error',
                                msg: result.errorMsg
                            });
                        }
                    },'json');
                }
            });
        }
    }
</script>

</body>
</html>